﻿function fileQueueError(file, errorCode, message) {
    try {
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                break;
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                break;
            case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
                alert("文件已达到最大数量");
                break;
            default:
                break;
        }
    } catch (ex) {
        this.debug(ex);
    }
}
function fileQueued(file) {
    try {
        var html = '<div class="fileItem" id="' + file.id + '"><span class="fileName">' + file.name + '</span><span class="progressStatus">文件就绪</span>';
        html += '<div class="progressBar"></div></div>';
        $("#goods_upload_progress").append(html);
    } catch (e) {
        this.debug(e);
    }
}
function uploadStart(file) {
    try {

    } catch (e) {
        this.debug(e);
    }
}
function uploadProgress(file, bytesLoaded) {
    try {
        var percent = Math.ceil((bytesLoaded / file.size) * 100);
        if (percent > 100) percent = 100;

        $("#" + file.id + " .progressStatus").html("上传进度：" + percent + "%");
        $("#" + file.id + " .progressBar").width(percent + "%");
    } catch (ex) {
        this.debug(ex);
    }
}

function uploadSuccess(file, serverData) {
    try {
        var data = eval('(' + serverData + ')');
        if (data.IsUpload) {
            $("#" + file.id + " .progressStatus").html("上传成功");
            //添加图片到页面
            add_uploadedfile(data);
        } else {
            $("#" + file.id + " .progressStatus").html("文件上传失败");
        }

        $("#" + file.id).fadeOut(1000, function () { $(this).remove() });
    } catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
    try {
        if (this.getStats().files_queued > 0) {
            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}

function uploadError(file, errorCode, message) {
    try {

    } catch (ex3) {
        this.debug(ex3);
    }

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        if ($("#goods_images > li").size() >= 5) {
            alert("最多只能上传5张主图片");
            return false;
        }

        if (numFilesQueued > 0) {
            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}

//主图片上传完成
function add_uploadedfile(fileData) {
    $('#goods_images').append('<li><img src="' + fileData.Path + '" width="55" height="55" /><input name="MajorImages" type="hidden" value="' + fileData.Path + '" /></li>');
    if ($('#big_goods_image').attr('src').indexOf('default_goods_image.jpg') != -1) {
        $('#big_goods_image').attr('src', fileData.Path);
    }

    if (window['GOODS_SWF_UPLOAD'].getStats().files_queued == 0) {
        $("#uploader").hide();
        $("#open_uploader > b").attr('class', 'hide');
    }
}

//初始化一个上传控件
function InitSwfUploadFile(options) {
    var settings = {
        upload_url: 'upload.aspx',
        flash_url: '/js/swfupload.swf',
        post_params: {},
        file_size_limit: "5 MB",
        file_upload_limit: 5,
        file_queue_limit: 5,
        file_types: "*.gif;*.jpg;*.jpeg;*.png",
        custom_settings: {
            upload_target: "goods_upload_progress",
            if_multirow: 1
        },

        // Button Settings
        button_image_url: '/Style/Images/member/SmallSpyGlassWithTransperancy_17x18.png',
        button_width: 86,
        button_height: 18,
        button_text: '<span class="button">批量上传</span>',
        button_text_style: '.button {font-family: Helvetica, Arial, sans-serif; font-size: 12pt; font-weight: bold; color: #3F3D3E;} .buttonSmall {font-size: 10pt;}',
        button_text_top_padding: 0,
        button_text_left_padding: 18,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_cursor: SWFUpload.CURSOR.HAND,
        button_placeholder_id: "spanButtonPlaceholder",

        //Event
        file_queue_error_handler: fileQueueError,
        file_queued_handler: fileQueued,
        file_dialog_complete_handler: fileDialogComplete,
        upload_start_handler: uploadStart,
        upload_progress_handler: uploadProgress,
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccess,
        upload_complete_handler: uploadComplete,

        //Debug
        debug: false
    };

    jQuery.extend(settings, options);
    return new SWFUpload(settings);
}


function initEditImages() {
    $("#goods_img_list_content li").mouseover(function () {
        $(this).find('.tips').show();
    }).mouseout(function () {
        $(this).find('.tips').hide();
    }).click(function () {
        var editor = CKEDITOR.instances.Description;
        var value = '<img src="'+$(this).find("img").attr('src')+'" />';
        editor.focus();
        if (editor.mode == 'wysiwyg') {
            editor.insertHtml(value);
        }
        else
            alert('请回到可见编辑模式');
    });
}